//
//  EditMobileViewController.m
//  SouthChina
//
//  Created by kc on 15/7/4.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "EditMobileViewController.h"
#import "BaseViewController.h"
#import "PersonalInforViewController.h"

@interface EditMobileViewController ()<UITextFieldDelegate>{
    NSString *sMobileOld;
    NSString *slinkstatusOld;
}
@property (strong, nonatomic) IBOutlet UISwitch *friendSwitchBtn;
@property (strong, nonatomic) IBOutlet UISwitch *groupSwitchBtn;
@property (strong, nonatomic) IBOutlet UITextField *textmobile; //手机号码
@property (strong, nonatomic) IBOutlet UILabel *labelflag;
@property (strong, nonatomic) NSArray * titles;
@end

@implementation EditMobileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"手机" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"EditMobileViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
     _textmobile.delegate = self;
    NSString *usermoible = [DE getGlobalVar:LOGIN_USER_MOBILE] == nil?@"":[DE getGlobalVar:LOGIN_USER_MOBILE];
    _textmobile.text = usermoible;
    
    sMobileOld = usermoible;
    NSString *linkinfostate = [DE getGlobalVar:LOGIN_USER_LINKINFOSTATE] == nil?@"":[DE getGlobalVar:LOGIN_USER_LINKINFOSTATE];
    // 1、好友可以见 2 群组可以见（除好友，陌生人）3 任何人都可见 0 任何人都不可见
    if ([@"1" isEqualToString:linkinfostate]) {
        [_friendSwitchBtn setOn:YES];
        [_groupSwitchBtn setOn:NO];
        } else if ([@"2" isEqualToString:linkinfostate]) {
        [_friendSwitchBtn setOn:NO];
        [_groupSwitchBtn setOn:YES];
        } else if ([@"0" isEqualToString:linkinfostate]) {
        [_friendSwitchBtn setOn:NO];
        [_groupSwitchBtn setOn:NO];
       } else {
        [_friendSwitchBtn setOn:YES];
        [_groupSwitchBtn setOn:YES];
       }
    slinkstatusOld = linkinfostate;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return  YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return  YES;
}

//判断字符中为纯数字
- (BOOL)isPureInt:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return[scan scanInt:&val] && [scan isAtEnd];
}


-(void)btnBack{
    if( ![self isPureInt:_textmobile.text]){
        //回退到上一级页面
        [self.navigationController popViewControllerAnimated:YES];
        PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
        [PersonalInfoviewC showAlerHUD:@"修改失败，号码只能为纯数字"];
        return;
    }
    
    NSString * linkStat = @"";
    bool isGroupButtonOn = [_groupSwitchBtn isOn];
    bool isFriendButtonOn = [_friendSwitchBtn isOn];
    
    if (isGroupButtonOn && isFriendButtonOn) {
        linkStat = @"3";
    }else if (isGroupButtonOn && !isFriendButtonOn)
    {
        linkStat = @"2";
    }else if (!isGroupButtonOn && isFriendButtonOn)
    {
        linkStat = @"1";
    }else
    {
        linkStat = @"0";
    }
    
    if([sMobileOld isEqualToString:_textmobile.text] && [slinkstatusOld isEqualToString:linkStat])
    {
       [super btnBack];
        return;
    }
    
    
     NSLog(@"linkStat moible。。。。。。%@",linkStat);
    
    NSDictionary *params = @{@"phone":_textmobile.text,@"linkInfoState":linkStat};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"updateUserInfo" params:params callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        //UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success && [data objectForKey:@"result"])
        {
            [DE setGlobalVar:LOGIN_USER_MOBILE value:_textmobile.text];
            [DE setGlobalVar:LOGIN_USER_LINKINFOSTATE value:linkStat];
            //回退到上一级页面
            [self.navigationController popViewControllerAnimated:YES];
            PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
            [PersonalInfoviewC showAlerHUD:@"修改手机号码成功"];
            
        }else
        {
            [self.navigationController popViewControllerAnimated:YES];
            PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
            [PersonalInfoviewC showAlerHUD:@"修改手机号码失败"];
        }
        
    }];
    [_textmobile resignFirstResponder]; //点击完成按钮，键盘隐藏
    
    [super btnBack];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
